/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: May 23, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.hce.clustering.similarity.attribute;

import java.util.Vector;

public abstract class BasicSimilarity implements SimilarityComparator {
    /**
     * by default weight =1
     * @param weights
     * @param i
     * @return
     */
    protected  float getWeight(Vector weights, int i) {
        float weight=1.0f;
        if(weights!=null){ //if weight =null, simply return 1
            Float wght = (Float) weights.get(i);
            if (wght!=null&&Float.isNaN(wght.floatValue() )){
                weight=wght.floatValue() ;
            }
        }
        return weight;
    }
     /**
     * by default weight =1
     * @param weights
     * @param i
     * @return
     */
    protected  float getWeight(float[] weights, int i) {
        float weight=1.0f;
        if(weights!=null){
            float wght = weights[i];
            if (Float.isNaN(wght )){
                weight=wght ;
            }
        }
        return weight;
    }

     /**
     * by default weight =1
     * @param weights
     * @param i
     * @return
     */
    protected  double getWeight(double[] weights, int i) {
        double weight=1.0f;
        if(weights!=null){
            double wght = weights[i];
            if (Double.isNaN(wght )){
                weight=wght ;
            }
        }
        return weight;
    }

    /**
     * convert distance to similarity
     * @param d
     * @return
     */
    public static double convert2Similarity(double d) {
        return (1 - (d / (1 + d)));
    }
}
